package doc2db.interfaces.action.save;

import doc2db.UserProp;
import doc2db.interfaces.mainWindow.MyJEditorPane;
import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

/**
 *
 * @author yura
 */
public class SaveDocx implements Saver {
    /** Edit panel whith date */
    private MyJEditorPane medit ;

    /** Text of error if it hapen */
    private String error = "ancnown";

    /**
     * this.medit = medit;
     * @param medit edit panel whith date
     */
    public SaveDocx(MyJEditorPane medit) {
        this.medit = medit;
    }

    @Override
    public String err(){
        return error;
    }

    /**
     *  Edit panel whith date
     * @return Edit panel whith date
     */
    public MyJEditorPane getMedit() {
        return medit;
    }

    /**
     *  Edit panel whith date
     * @param medit Edit panel whith date
     */
    public void setMedit(MyJEditorPane medit) {
        this.medit = medit;
    }


    @Override
    public boolean save (String fname){
        return saveDoc(fname);
    }

    private boolean saveDoc (String fname){
        String mfname;
        if (new File(fname).exists()){
            mfname = "" + fname;
        } else if (fname.endsWith(".docx") | fname.endsWith(".DOCX")) {
            mfname = "" + fname;
        } else {
            mfname = fname + ".docx";
        }

        final String[][] data = medit.table;
        final String[] head = medit.head;
        int row = data.length;
        int col = data[0].length;
        int index = 0;
        if (head != null && head.length > 0 && head.length == data[0].length) {
            index ++;
        }

        final String fontFamily = "" + UserProp.textFont.getFamily();
        final int fontSize = UserProp.textFont.getSize();
        XWPFDocument doc = new XWPFDocument();
        XWPFTable table = doc.createTable(row + index, col);
        if (index > 0){
            XWPFTableRow r = table.getRow(0);
            for (int i = 0; i < head.length; i++) {
                /*XWPFTableCell c = r.getCell(i);
                XWPFParagraph p1 = doc.createParagraph();
                XWPFRun r1 = p1.createRun();
                r1.setBold(true);
                r1.setText("" + head[i]);
                r1.setFontFamily(fontFamily);
                r1.setFontSize(fontSize);
                c.setParagraph(p1);*/
                r.getCell(i).setText("" + head[i]);
            }
        }
        for (int i = 0; i < row; i++) {
            XWPFTableRow r = table.getRow(i + index);
            for (int j = 0; j < col; j++) {
                /*XWPFTableCell c = r.getCell(j);
                XWPFParagraph p1 = doc.createParagraph();
                XWPFRun r1 = p1.createRun();
                r1.setText("" + data[i][j]);
                r1.setFontFamily(fontFamily);
                r1.setFontSize(fontSize);
                c.setParagraph(p1);*/
                r.getCell(j).setText("" + data[i][j]);
            }
        }

        /*Записываем в файл*/
        try {
            FileOutputStream out = new FileOutputStream(mfname);
            doc.write(out);
            out.flush();
            out.close();
        } catch (Exception e) {
            error = "" + e;
            return false;
        }
        return true;
    }

}
